package net.melon.sysres.inst.struts2;

import java.io.File;

import net.esj.basic.core.engine.config.Configuration;
import net.esj.basic.core.engine.util.FileUploadUtils;
import net.esj.basic.plugins.view.FacadeAware;
import net.esj.basic.utils.Validators;
import net.esj.basic.utils.i18n.MessageCode;
import net.esj.basic.utils.view.BaseJsonMessage.MessageLevel;
import net.esj.basic.view.BaseAction;
import net.melon.sysres.Configer;
import net.melon.sysres.Constants;
import net.melon.uploader.Uploader;
import net.melon.uploader.service.FileNameRule;
import net.melon.uploader.service.PathRule;

public class UploadAction extends BaseAction implements MessageCode,Constants,FacadeAware{

	private String parent;
	
	/*private File file;
	
	private String fileFileName;
	private String fileContextType;*/
	
	private String template;
	private String theme;
	private String namespace; 
	
	private Uploader uploader;
	
	@Override
	public String execute() throws Exception {
		/*File savefile = new File(new File(Configer.getSysUploadRootPath()+parent), fileFileName);
		if (!savefile.getParentFile().exists())
			savefile.getParentFile().mkdirs();
		FileUtils.copyFile(file, savefile);*/
		try{
			getUploader().upload(getRequest());
			//FileUploadUtils.upload(getRequest(), getResponse(), Configer.getSysUploadRootPath()+parent, false);
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE), getText(ACTION_SUCCESS), MessageLevel.INFO,"html");
		}catch(Exception e){
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE), getText(e.getMessage()), MessageLevel.INFO,"html");
		}
		return null;
	}
	
	public void countPercent(){
		Double d = FileUploadUtils.getUploadPercent(getRequest());
		writeJson(((int)(d*100))/100.0);
	}

	public void setParent(String parent) {
		this.parent = parent;
	}

	/*public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public void setFileContextType(String fileContextType) {
		this.fileContextType = fileContextType;
	}*/
	@Override
	protected String[] getDefaultSources() {
		String str  = configuration.getProperty(DEFAULT_MELON_MESSAGE, null);
		if(Validators.isEmpty(str)){
			return null;
		}
		return new String[]{str};
	}
	
	protected Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	private Uploader getUploader(){
		if(uploader==null){
			uploader = new Uploader(new BasePathRule(), new BaseFileNameRule(), Configer.UPLOADER_DEFAULT_DIR);
		}
		return uploader;
	}
	
	private class BasePathRule implements PathRule{

		@Override
		public String getRelativePath(File file, String filename,
				Object... args) {
			return parent;
		}
	}
	
	private class BaseFileNameRule implements FileNameRule{

		@Override
		public String getFileName(File file, String filename, Object... args) {
			return filename;
		}
		
	}
}
